package com.jymj.service.impl;

import com.jymj.dao.RtRaiseDao;
import com.jymj.entity.RtRaiseEntity;
import com.jymj.entity.view.RtRaiseTask;
import com.jymj.service.RtRaiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RtRaiseServiceImpl implements RtRaiseService {

    @Autowired
    private RtRaiseDao rtRaiseDao;

    @Override
    public void save(RtRaiseEntity rtRaiseEntity) {
        rtRaiseDao.save(rtRaiseEntity);
    }

    @Override
    public void update(RtRaiseEntity rtRaiseEntity) {
        rtRaiseDao.update(rtRaiseEntity);
    }

    @Override
    public void updateParentCost(RtRaiseEntity rtRaiseEntity) {
        //先根据taskid去视图查询 rtRaise
        Map map = new HashMap();
        map.put("taskid",rtRaiseEntity.getTaskid());
        map.put("pjid",rtRaiseEntity.getPid());
        RtRaiseTask rtRaise = rtRaiseDao.queryObject(map);
        //判断父id是否为0 是直接return
        if(rtRaise==null || rtRaise.getPtfatherid()==0){
            return;
        }
        // else 判断id不为null 查询父
        if(rtRaise.getPtfatherid()!=null){
            Map map1 = new HashMap();
            map1.put("taskid",rtRaise.getPtfatherid());
            map1.put("pjid",rtRaise.getPtpjid());
            RtRaiseTask fstherRtRaise = rtRaiseDao.queryObjectParent(map1);
            if(fstherRtRaise!=null){
                //fstherRtRaise赋值给RtRaiseEntity
                RtRaiseEntity rtRaiseEntity1 = new RtRaiseEntity();
                rtRaiseEntity1.setPid(fstherRtRaise.getPtpjid());
                rtRaiseEntity1.setTaskid(fstherRtRaise.getPttaskid());
                //判断父rtRaise id为null  添加  else 修改
                if(fstherRtRaise.getId()==null){
                    this.save(rtRaiseEntity1);
                }
                //修改父 =子之和
                this.updateParentN(rtRaiseEntity1);
                updateParentCost(rtRaiseEntity1);
            }
        }
    }

    private void updateParentN(RtRaiseEntity rtRaiseEntity1) {
        Map map = new HashMap();
        map.put("taskid", rtRaiseEntity1.getTaskid());
        map.put("pjid", rtRaiseEntity1.getPid());
        rtRaiseDao.updateParentN(map);
    }

    @Override
    public List<RtRaiseEntity> queryList(Map<String, Object> map) {
        return rtRaiseDao.queryList(map);
    }
}
